package gui.client;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;

import system.FieldValidator;
import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.Client;
import vcpClasses.SubscribedClient;

import javax.swing.JEditorPane;
import javax.swing.JTextField;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import control.LocalOpertaionController;
import control.LoginController;
import control.OrderController;
/**
 * this class is used for entering vehicles of subscribed clients. 
 * @author G8
 *
 */
@SuppressWarnings("serial")
public class SubscribedClientScreen extends JPanel implements ActionListener
{
	public JButton btnLogOut;
	public JButton btnBack;
	public JButton btnEnter;
	private JTextField VehicleNumber;
	private JTextField subNumber;
	private JLabel lblVehicleNumber;
	private LocalOpertaionController localOpControl;
	/**
	 *Track Orde Screen Constructor 
	 */
	public SubscribedClientScreen()
	{
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize()
	{
		localOpControl = new LocalOpertaionController(null);
		
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Subscribed Client Screen", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(AdvanceOrderScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogOut = new JButton("Log Out");
		btnLogOut.setSize(150, 30);
		btnLogOut.setBounds(393, 22, 78, 35);
		btnLogOut.addActionListener(this);
		add(btnLogOut);
		
		btnEnter = new JButton("Enter Vehicle");
		btnEnter.setBounds(182, 220, 120, 35);
		btnEnter.addActionListener(this);
		add(btnEnter);
		
		VehicleNumber = new JFormattedTextField();
		VehicleNumber.setBounds(265, 171, 90, 20);
		add(VehicleNumber);
		
		lblVehicleNumber = new JLabel("Vehicle Number :");
		lblVehicleNumber.setBounds(87, 174, 160, 14);
		add(lblVehicleNumber);
		
		JLabel lblSubNum = new JLabel("Subscription Number :");
		lblSubNum.setBounds(87, 140, 160, 14);
		add(lblSubNum);
		
		subNumber = new JFormattedTextField();
		subNumber.setBounds(265, 137, 90, 20);
		add(subNumber);
	}
	
	
	
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{

		if (e.getSource() == btnEnter)
		{
			subNumber.setBackground(Color.WHITE);
			VehicleNumber.setBackground(Color.WHITE);
			
			if (FieldValidator.checkNumeric(subNumber.getText()) && FieldValidator.checkNumeric(VehicleNumber.getText()))
			{
				SubscribedClient subscribed = new SubscribedClient();
				subscribed.setClientNumber(((Client)VCP.currentUser).getClientNumber());
				subscribed.setVehicleNumber(Integer.parseInt(VehicleNumber.getText()));
				subscribed.setSubscriptionNumber(Integer.parseInt(subNumber.getText()));
				subscribed.setSidParkingLoaction(0);
				subscribed.setClientLeaveDate(CalculateDate());
				SystemTypes subToEnter = new SystemTypes(subscribed,null);
				SystemTypes enterResult = localOpControl.enterCar(subToEnter);
				if (enterResult.getSysMode().equals(SystemMode.OK))
					JOptionPane.showMessageDialog(null, enterResult.getObj().toString(), "Success",JOptionPane.INFORMATION_MESSAGE);
				else
					JOptionPane.showMessageDialog(null,enterResult.getObj().toString(), "Error",JOptionPane.ERROR_MESSAGE);
			}
			else{
				if (!FieldValidator.checkNumeric(subNumber.getText())) subNumber.setBackground(Color.RED);
				if (!FieldValidator.checkNumeric(VehicleNumber.getText())) VehicleNumber.setBackground(Color.RED);
				JOptionPane.showMessageDialog(null,"Invalid field(s) detected !", "Error",JOptionPane.ERROR_MESSAGE);
			}
		}
		
		if (e.getSource() == btnBack)
		{
			VCP.setScreen(this, VCP.prevScreen);
			VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
			VCP.prevScreen.setLayout(null);
			VCP.frame.setBounds(VCP.prevScreen.getBounds());
		}
		
		if (e.getSource() == btnLogOut)
		{
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	public String CalculateDate(){
		DateTimeFormatter fmt1 = DateTimeFormat.forPattern("yyyy-MM-dd");
		String date;
		date = fmt1.print((new DateTime()).plusDays(1));
		return date;
	}
}
